﻿using SuperX.Common.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperX.Common.Helper
{
    /// <summary>
    /// 设备硬件信息获取帮助工具
    /// </summary>    
    public static class MachineHardwareInfoUtil
    {
        public static void RegisterHardwareInfoInstance(IHardwareInfo hardwareInfo)
        {
            MachineHardwareInfoUtil._hardwareInfo = hardwareInfo;
        }

        /// <summary>
        /// 获取物理网卡信息
        /// </summary>
        /// <returns></returns>
        public static List<string> GetNetAdapterMacAddress()
        {
            return MachineHardwareInfoUtil._hardwareInfo.GetNetAdapterMacAddress();
        }

        /// <summary>  
        /// 获取CpuID  
        /// </summary>  
        /// <returns>CpuID</returns>  
        public static string GetCpuID()
        {
            return MachineHardwareInfoUtil._hardwareInfo.GetCpuID();
        }

        /// <summary>  
        /// 获取主板id  
        /// </summary>  
        /// <returns></returns>   
        public static string GetMotherBoardID()
        {
            return MachineHardwareInfoUtil._hardwareInfo.GetMotherBoardID();
        }

        // 
        private static List<string> GetAllCompareMotherBoardId(string str)
        {
            return new List<string>
            {
                str,
                (str != null) ? str.Replace(" ", string.Empty) : null
            };
        }

        // 
        private static List<string> GetAllCompareCpuId(string str)
        {
            List<string> list = new List<string>
            {
                str
            };
            if (str.Length % 2 != 0)
            {
                return list;
            }
            string text = string.Empty;
            for (int i = 0; i < str.Length; i += 2)
            {
                text = text.Insert(0, str.Substring(i, 2));
            }
            list.Add(text);
            return list;
        }

        // 
        internal static List<string> GetHardwareInfo(bool isCompare)
        {
            List<string> hardwareInfo = MachineHardwareInfoUtil._hardwareInfo.GetHardwareInfo();
            if (hardwareInfo != null && hardwareInfo.Count > 0)
            {
                return hardwareInfo;
            }
            List<string> netAdapterMacAddress = MachineHardwareInfoUtil.GetNetAdapterMacAddress();
            string motherBoardId = MachineHardwareInfoUtil.GetMotherBoardID();
            string cpuId = MachineHardwareInfoUtil.GetCpuID();
            if (isCompare)
            {
                List<string> list = new List<string>();
                List<string> allCompareMotherBoardId = MachineHardwareInfoUtil.GetAllCompareMotherBoardId(motherBoardId);
                List<string> allCompareCpuId = MachineHardwareInfoUtil.GetAllCompareCpuId(cpuId);
                foreach (string str in netAdapterMacAddress)
                {
                    foreach (string str2 in allCompareMotherBoardId)
                    {
                        foreach (string str3 in allCompareCpuId)
                        {
                            list.Add(str + str2 + str3);
                        }
                    }
                }
                return list;
            }
            return (from item in netAdapterMacAddress
                    select item + motherBoardId + cpuId).ToList<string>();
        }

        private static IHardwareInfo _hardwareInfo;
    }
}
